package com.shaodongbo.webview;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends AppCompatActivity {
    private WebView my_webview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        my_webview = findViewById(R.id.my_webview);

//        my_webview.loadUrl("http://www.baidu.com");
//        my_webview.loadUrl("http://ica1.gdcp.cn/web/loadWebPage.do?1_649_44015038150_3");

        //加载assets目录下的本地网页
//        my_webview.loadUrl("file:///android_asset/test.html");

        //加载html文本内容

        String htmlContent = "<html>\n" +
                "<title>本地网页</title>\n" +
                "<body>\n" +
                "Hello World<br/>\n" +
                "<img src=\"file:///android_asset/a.png\">\n" +
                "</body>\n" +
                "</html>";

//        my_webview.loadData(htmlContent, "text/html", "utf-8");
        my_webview.loadDataWithBaseURL(null, htmlContent, "text/html", "utf-8", null);
        
        //问题1 网页跳转的时候打开了系统浏览器
        my_webview.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });

        findViewById(R.id.go_foward).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (my_webview.canGoForward()){
                    my_webview.goForward();
                }
            }
        });
    }

    //点击返回键退出程序
    @Override
    public void onBackPressed() {
        Log.e("MainActivity", "onBackPressed");
        if (my_webview.canGoBack()){
            my_webview.goBack();
        } else {
            super.onBackPressed();
        }
    }
}
